home *** CD-ROM | disk | FTP | other *** search
- /*
- File: USBEnetDriver.c
-
- Contains: xxx put contents here xxx
-
- Version: xxx put version here xxx
-
- Copyright: © 1998-2000 by Apple Computer, Inc., all rights reserved.
-
- File Ownership:
-
- DRI: xxx put dri here xxx
-
- Other Contact: xxx put other contact here xxx
-
- Technology: xxx put technology here xxx
-
- */
-
- #include <DriverServices.h>
-
- #include "USBEnet.h"
- #include "USBEnetDriver.h"
- #include "ShimEnetHAL.h"
-
- enum
- {
- kUSBv12 = 0x01200000
- };
-
- static EnetPB gEnetGlobals;
- static USBPB syncPB, interruptPB, stallPB, delayPB;
- static OSStatus configerr = noErr;
- static volatile Boolean configured = false;
- static UInt16 errCount;
- UInt16 statusData[8];
- UInt8 Intrretries = 0;
- UInt32 usbVersion = 0;
- Boolean gUSBVersionNeedsBulkFixPresent;
- CFragConnectionID ConnID; // Need to remember this
-
- /************************************************************************************/
- //
- // CheckUSBVersion
- //
- // Determines whether it's USB1.2 (less requires doing safe bulk calls)
- //
- /************************************************************************************/
-
- void CheckUSBVersion(void)
- {
- OSStatus err = 0;
-
- TraceMessage(0, "\pEntering CheckUSBVersion");
-
- err = Gestalt('usbv', (long*)&usbVersion);
-
- if (err == noErr)
- gUSBVersionNeedsBulkFixPresent = (usbVersion < kUSBv12);
- }
-
- /************************************************************************************/
- //
- // USBStatus
- //
- // Determines which status call to make.
- //
- /************************************************************************************/
-
- void USBStatus(UInt32 level, USBDeviceRef ref, void *pointer, UInt32 value)
- {
-
- if (usbVersion < kUSBv12)
- {
- USBExpertStatus(ref, pointer, value);
- } else {
- USBExpertStatusLevel(level, ref, pointer, value);
- }
- }
-
- /************************************************************************************/
- //
- // AddToRcvList
- //
- // Adds the buffer to the receive list.
- //
- /************************************************************************************/
-
- void AddToRcvList(UInt32 bufindx)
- {
-
- TraceMessage(0,kDrvName"- Entering AddToRcvList");
-
- Enqueue((QElemPtr)&gGlobals->inBuf[bufindx], &gGlobals->rcvq);
-
- }
-
- /************************************************************************************/
- //
- // RemoveFromRcvList
- //
- // Removes the next buffer from the receive list.
- //
- /************************************************************************************/
-
- UInt8* RemoveFromRcvList(UInt32 *indx)
- {
-
- OSErr err;
- BuffersPtr next;
-
- TraceMessage(0,kDrvName"- Entering RemoveFromRcvList");
-
- next = (BuffersPtr)gGlobals->rcvq.qHead; // get first buffer off the list
- if (next == nil)
- return nil; // empty list
-
- err = Dequeue((QElemPtr)next, &gGlobals->rcvq);
- if (err)
- return nil; // shouldn't happen
-
- *indx = next->indx;
- return (UInt8*)&next->Buffer;
- }
-
- /************************************************************************************/
- //
- // immediateError
- //
- // Determines whether it's an error or just pending.
- //
- /************************************************************************************/
-
- static Boolean immediateError(OSStatus err)
- {
- return ((err != kUSBPending) && (err != noErr));
- }
-
- UInt16 cnter = 0;
- UInt8 savOther = 0;
- /************************************************************************************/
- //
- // ConfigurationHandler
- //
- // Configures the USB Device (Ethernet).
- //
- /************************************************************************************/
-
- static void ConfigurationHandler(USBPB *pb)
- {
-
- // TraceMessage(0, kDrvName"- Entering ConfigurationHandler");
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Entering ConfigurationHandler", pb->usbRefcon);
-
- if(pb->usbStatus != noErr)
- {
- if((gEnetGlobals.onError == kReset) && (gEnetGlobals.retries > 0))
- {
- /* no idea what to do now?? */
- USBExpertFatalError(gEnetGlobals.deviceRef, pb->usbStatus, gEnetGlobals.errorString, pb->usbRefcon);
-
- /* Mark port as errored */
- configured = true;
- configerr = pb->usbStatus;
- }
- else
- {
- StatusMessage(gEnetGlobals.deviceRef, gEnetGlobals.errorString, pb->usbStatus);
- pb->usbRefcon = gEnetGlobals.onError;
-
- USBClearPipeStallByReference(pb->usbReference);
-
- /* we'll delay coming back to here */
- gEnetGlobals.retries--;
-
- pb->usbReqCount = 0;
- USBDelay(pb);
- }
- return;
- }
-
- gEnetGlobals.onError = kReset;
-
- do{switch(pb->usbRefcon++)
- {
- case kCommConfig:
-
- /* Need to find an interface with a communication class in it */
- pb->usbClassType = kUSBCommClass;
- pb->usbSubclass = 6; /* Ethernet Model */
- pb->usbProtocol = 0;
- pb->usb.cntl.WValue = 0;
- pb->usb.cntl.WIndex = 0;
- pb->usbReqCount = 0;
- pb->usbBuffer = nil;
- pb->usbFlags = 0;
-
- if (gEnetGlobals.retries > 0)
- {
- gEnetGlobals.onError = kCommConfig;
- } else {
- gEnetGlobals.onError = kReset;
- }
- noteError(kDrvName"- Finding the Comm Class Interface");
- if(immediateError(configerr = USBFindNextInterface(pb)))
- {
- USBExpertFatalError(gEnetGlobals.deviceRef, configerr, gEnetGlobals.errorString, 0);
- configured = true;
- }
-
- break;
-
- case kSetConfig:
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Configuration Number - ", pb->usb.cntl.WValue);
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Interface Number - ", pb->usb.cntl.WIndex);
-
- /* Remember the interface number */
- gEnetGlobals.interfacenum = pb->usb.cntl.WIndex;
-
- /* Open the device based on the config found */
- if (gEnetGlobals.retries > 0)
- {
- gEnetGlobals.onError = kSetConfig;
- } else {
- gEnetGlobals.onError = kReset;
- }
- noteError(kDrvName"- Setting the configuration");
- if(immediateError(configerr = USBSetConfiguration(pb)))
- {
- USBExpertFatalError(gEnetGlobals.deviceRef, configerr, gEnetGlobals.errorString, 0);
- configured = true;
- }
-
- break;
-
- case kGetCommInterface:
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Number of interfaces in Configuration - ", pb->usbOther);
-
- /* Get the interface reference number */
- if (gEnetGlobals.retries > 0)
- {
- gEnetGlobals.onError = kGetCommInterface;
- } else {
- gEnetGlobals.onError = kReset;
- }
- pb->usbOther = 0;
-
- noteError(kDrvName"- Getting Comm Class interface");
- if(immediateError(configerr = USBNewInterfaceRef(pb)))
- {
- USBExpertFatalError(gEnetGlobals.deviceRef, configerr, gEnetGlobals.errorString, 0);
- configured = true;
- }
-
- break;
-
- case kSetCommInterface:
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Comm. Interface Ref - ", pb->usbReference);
-
- /* Remember the interfaceRef */
- gEnetGlobals.interfaceRef = pb->usbReference;
-
- /* Set the interface (Comm) */
- pb->usb.cntl.BMRequestType = USBMakeBMRequestType(kUSBNone, kUSBStandard, kUSBInterface);
- pb->usb.cntl.BRequest = kUSBRqSetInterface;
- pb->usb.cntl.WValue = 0;
- pb->usb.cntl.WIndex = gEnetGlobals.interfacenum;
- pb->usbReqCount = 0;
- pb->usbBuffer = nil;
- if (gEnetGlobals.retries > 0)
- {
- gEnetGlobals.onError = kSetCommInterface;
- } else {
- gEnetGlobals.onError = kReset;
- }
-
- noteError(kDrvName"- Setting Comm Class interface");
- if(immediateError(configerr = USBDeviceRequest(pb)))
- {
- USBExpertFatalError(gEnetGlobals.deviceRef, configerr, gEnetGlobals.errorString, 0);
- }
- break;
-
- case kConfigureCommInterface:
-
- /* configure the Comm Class interface */
- if (gEnetGlobals.retries > 0)
- {
- gEnetGlobals.onError = kConfigureCommInterface;
- } else {
- gEnetGlobals.onError = kReset;
- }
-
- noteError(kDrvName"- Configuring the Comm Class interface");
- if(immediateError(configerr = USBConfigureInterface(pb)))
- {
- USBExpertFatalError(gEnetGlobals.deviceRef, configerr, gEnetGlobals.errorString, 0);
- configured = true;
- }
-
- break;
-
- case kGetInterruptEndpoint:
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Number of pipes in Interface - ", pb->usbOther);
-
- /* Get the Comm Class interrupt endpoint */
- pb->usbFlags = kUSBIn;
- pb->usbClassType = kUSBInterrupt;
- if (gEnetGlobals.retries > 0)
- {
- gEnetGlobals.onError = kGetInterruptEndpoint;
- } else {
- gEnetGlobals.onError = kReset;
- }
-
- noteError(kDrvName"- Finding the interrupt endpoint");
- if(immediateError(configerr = USBFindNextPipe(pb)))
- {
- USBExpertFatalError(gEnetGlobals.deviceRef, configerr, gEnetGlobals.errorString, 0);
- configured = true;
- }
-
- break;
-
- case kStartGetDescriptors:
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Interrupt In Ref - ", pb->usbReference);
-
- /* Interrupt Endpoint is open - remember the ref */
- gEnetGlobals.interrupt = pb->usbReference;
-
- /* Set up to get the associated descriptors */
- pb->usbReference = gEnetGlobals.interfaceRef;
- pb->usb.cntl.WIndex = 0;
- pb->usbReqCount = sizeof(gEnetGlobals.FuncDesc);
- pb->usbBuffer = &gEnetGlobals.FuncDesc;
- pb->usbOther = CS_Interface;
- pb->usbFlags = 0;
- if (gEnetGlobals.retries > 0)
- {
- gEnetGlobals.onError = kStartGetDescriptors;
- } else {
- gEnetGlobals.onError = kReset;
- }
-
- noteError(kDrvName"- Set up for descriptors");
- if(immediateError(configerr = USBFindNextAssociatedDescriptor(pb)))
- {
- USBExpertFatalError(gEnetGlobals.deviceRef, configerr, gEnetGlobals.errorString, 0);
- configured = true;
- }
-
- break;
-
- case kGetDescriptors:
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Descriptor", pb->usb.cntl.WIndex);
- LogData(kUSBNone, pb->usbActCount, (UInt8 *)pb->usbBuffer);
-
- if ((gEnetGlobals.FuncDesc.bDescriptorType == CS_Interface) &&
- (gEnetGlobals.FuncDesc.bDescriptorSubtype == Ethernet_Functional_Descriptor))
- {
- continue;
- } else {
-
- pb->usbRefcon--; // Need to come back here
- pb->usbFlags = 0;
- if (gEnetGlobals.retries > 0)
- {
- gEnetGlobals.onError = kGetDescriptors;
- } else {
- gEnetGlobals.onError = kReset;
- }
-
- noteError(kDrvName"- Get next descriptor");
- if(immediateError(configerr = USBFindNextAssociatedDescriptor(pb)))
- {
- USBExpertFatalError(gEnetGlobals.deviceRef, configerr, gEnetGlobals.errorString, 0);
- configured = true;
- }
- }
- break;
-
- case kSetDataConfig:
-
- pb->usb.cntl.WValue = 0;
- pb->usb.cntl.WIndex = 0;
- pb->usbReqCount = 0;
- pb->usbActCount = 0;
- pb->usbBuffer = nil;
- pb->usbOther = 0xff;
- continue;
-
- break;
-
- case kDataConfig:
-
- /* Need to find an interface with a data class in it */
- pb->usbClassType = kUSBDataClass;
- pb->usbSubclass = 0;
- pb->usbProtocol = 0;
- pb->usbReference = gEnetGlobals.deviceRef; // Set it back to the device
- pb->usbFlags = 0;
- if (gEnetGlobals.retries > 0)
- {
- gEnetGlobals.onError = kDataConfig;
- } else {
- gEnetGlobals.onError = kReset;
- }
-
- noteError(kDrvName"- Finding the Data Interface");
- if(immediateError(configerr = USBFindNextInterface(pb)))
- {
- USBExpertFatalError(gEnetGlobals.deviceRef, configerr, gEnetGlobals.errorString, 0);
- configured = true;
- }
-
- break;
-
- case kGetDataInterface:
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Configuration Number - ", pb->usb.cntl.WValue);
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Interface Number - ", pb->usb.cntl.WIndex);
-
- /* Remember the interface number */
- gEnetGlobals.interfacenum = pb->usb.cntl.WIndex;
-
- /* Get the interface reference number */
- if (gEnetGlobals.retries > 0)
- {
- gEnetGlobals.onError = kGetDataInterface;
- } else {
- gEnetGlobals.onError = kReset;
- }
- pb->usbOther = 0;
-
- noteError(kDrvName"- Getting Data Class interface");
- if(immediateError(configerr = USBNewInterfaceRef(pb)))
- {
- USBExpertFatalError(gEnetGlobals.deviceRef, configerr, gEnetGlobals.errorString, 0);
- configured = true;
- }
-
- break;
-
- case kConfigureDataInterface:
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Data Interface Ref - ", pb->usbReference);
-
- /* configure the Data Class interface */
- if (gEnetGlobals.retries > 0)
- {
- gEnetGlobals.onError = kConfigureDataInterface;
- } else {
- gEnetGlobals.onError = kReset;
- }
-
- noteError(kDrvName"- Configuring the Data Class interface");
- if(immediateError(configerr = USBConfigureInterface(pb)))
- {
- USBExpertFatalError(gEnetGlobals.deviceRef, configerr, gEnetGlobals.errorString, 0);
- configured = true;
- }
-
- break;
-
- case kGetBulkOutEndpoint:
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Number of pipes in Interface - ", pb->usbOther);
-
- if (pb->usbOther != 2) // need to find one with at least 2 (in and out)
- {
- if (cnter++ > 1) break;
-
- pb->usbRefcon = kDataConfig;
- savOther++;
- pb->usbOther = savOther;
- continue;
- }
-
- /* Find the out endpoint */
- pb->usbFlags = kUSBOut;
- pb->usbClassType = kUSBBulk;
- pb->usbSubclass = 0; /* Find the first one */
- if (gEnetGlobals.retries > 0)
- {
- gEnetGlobals.onError = kGetBulkOutEndpoint;
- } else {
- gEnetGlobals.onError = kReset;
- }
-
- noteError(kDrvName"- Finding the bulk out endpoint");
- if(immediateError(configerr = USBFindNextPipe(pb)))
- {
- USBExpertFatalError(gEnetGlobals.deviceRef, configerr, gEnetGlobals.errorString, 0);
- configured = true;
- }
-
- break;
-
- case kGetBulkInEndpoint:
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Bulk Out Endpoint - ", pb->usbReference);
-
- /* Out Endpoint is open - remember the ref */
- gEnetGlobals.bulkOut = pb->usbReference;
-
- /* Find the in endpoint */
- pb->usbFlags = kUSBIn;
- pb->usbClassType = kUSBBulk;
- pb->usbSubclass = 0; /* Find the first one */
- if (gEnetGlobals.retries > 0)
- {
- gEnetGlobals.onError = kGetBulkInEndpoint;
- } else {
- gEnetGlobals.onError = kReset;
- }
-
- noteError(kDrvName"- Finding the bulk in endpoint");
- if(immediateError(configerr = USBFindNextPipe(pb)))
- {
- USBExpertFatalError(gEnetGlobals.deviceRef, configerr, gEnetGlobals.errorString, 0);
- configured = true;
- }
-
- break;
-
- case kConfigDone:
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Bulk In Endpoint - ", pb->usbReference);
-
- /* In Endpoint is open - remember the ref */
- gEnetGlobals.bulkIn = pb->usbReference;
-
- // Let's go to task time just in case (if we're not already there - OT can be a bit ticklish)
- if (CurrentExecutionLevel() == 0)
- continue;
-
- StatusMessage(gEnetGlobals.deviceRef, "\pSwitching to task time", 0);
- pb->usbReqCount = 0;
- pb->usbFlags = kUSBTaskTimeFlag;
- USBDelay(pb);
-
- break;
-
- case kInstallDrvr:
- pb->usbReqCount = 0;
- pb->usbStatus = noErr;
- pb->usbBuffer = nil;
- pb->usbFlags = 0;
-
- if (CurrentExecutionLevel() != 0)
- StatusMessage(gEnetGlobals.deviceRef, "\pCaution - Installing OT driver at non-task time", 0);
-
- configerr = InstallShimDrvr(ConnID); // Could use Product ID string but for now we don't
- if (configerr == noErr)
- {
- StartStatusMonitor(gEnetGlobals.interrupt);
-
- // device is fully configured, now we can start reading/writing to the bulk endpoints
- syncPB.usbStatus = kAvailableStatus;
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Configuration complete.", 0);
-
- } else {
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Can't install driver.", configerr);
- }
- configured = true;
- break;
-
- default:
- noteError(kDrvName"- Internal Error unused case in Configuration handler");
- StatusMessage(gEnetGlobals.deviceRef, gEnetGlobals.errorString, (pb->usbRefcon-1));
- configured = true;
- configerr = -1;
- break;
- }
- break; /* only execute once, unless continue used */
- }while(1); /* so case can be reentered with a continue */
- }
-
- /************************************************************************************/
- //
- // ResetInterruptPB
- //
- // Re-initialize the interrupt PB.
- //
- /************************************************************************************/
-
- static void ResetInterruptPB(USBPB *pb)
- {
- pb->usbReqCount = sizeof(statusData);
- pb->usbBuffer = &statusData;
- pb->usbStatus = noErr;
- }
-
- /************************************************************************************/
- //
- // interruptCompletion
- //
- // Interrupt completion handler.
- //
- /************************************************************************************/
-
- static void interruptCompletion(USBPB *pb)
- {
- OSStatus err;
- UInt16 Notif;
- UInt32 upspeed;
- UInt16 *ups = (UInt16 *)&upspeed;
- UInt32 dwnspeed;
- UInt16 *dwns = (UInt16 *)&dwnspeed;
- ShimEnetGlobals *globals = gGlobals;
-
- TraceMessage(0, kDrvName"- Entering interruptCompletion");
-
- if (pb->usbStatus != kUSBAbortedError) // are we being asked to quit?
- {
- if (pb->usbStatus != noErr)
- {
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- interruptCompletion: Error ", pb->usbStatus);
- if (pb->usbStatus == kUSBEndpointStallErr)
- {
- ClearDevice();
- }
- USBClearPipeStallByReference(gEnetGlobals.interrupt);
-
- Intrretries++;
- if (Intrretries > 10)
- return; // we'll just give up for now
-
- DoDelay();
-
- } else {
- Intrretries = 0;
- TraceMessage(0, kDrvName"- Interrupt received");
- LogData(kUSBNone, pb->usbActCount, pb->usbBuffer);
-
- if (pb->usbActCount > 2)
- {
- Notif = (statusData[0] & 0x00FF);
- switch (Notif)
- {
- case kNetwork_Connection:
- (*gGlobals->ShimAsync) (gGlobals->ShimRef, EnetShim_Link, (HostToUSBWord(statusData[1])), 0);
- break;
-
- case kConnection_Speed_Change:
- ups[0] = statusData[4];
- ups[1] = statusData[5];
- dwns[0] = statusData[6];
- dwns[1] = statusData[7];
- (*gGlobals->ShimAsync) (gGlobals->ShimRef, EnetShim_Speed, (HostToUSBLong(upspeed)), (HostToUSBLong(dwnspeed)));
- break;
-
- default:
-
- break;
- }
- }
- }
- }
-
- ResetInterruptPB(pb);
-
- if(immediateError(err = USBIntRead(pb)))
- {
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- interruptCompletion: Couldn't queue interrupt read!", err);
- }
- }
-
- /************************************************************************************/
- //
- // StartStatusMonitor
- //
- // Kick off the interrupt mechanism.
- //
- /************************************************************************************/
-
- static void StartStatusMonitor(USBPipeRef interruptPipe)
- {
- USBPB *pb = &interruptPB;
- OSStatus err;
-
- TraceMessage(0, kDrvName"- Entering StartStatusMonitor");
-
- InitializePB(pb, interruptPipe, interruptCompletion);
-
- ResetInterruptPB(pb);
-
- if(immediateError(err = USBIntRead(pb)))
- {
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- StartStatusMonitor: Couldn't start interrupt read!", err);
- }
-
- }
-
- /************************************************************************************/
- //
- // syncCompletion
- //
- // Completion handler for all sync (setup) requests.
- //
- /************************************************************************************/
-
- static void syncCompletion(USBPB *pb)
- {
-
- TraceMessage(0, kDrvName"- Entering syncCompletion");
-
- if (pb->usbStatus != noErr)
- {
- USBExpertFatalError(gEnetGlobals.deviceRef, pb->usbStatus, kDrvName"- syncCompletion: Error for bRequest", pb->usb.cntl.BRequest);
- if (pb->usbStatus == kUSBEndpointStallErr)
- {
- // ClearDevice();
- }
- USBClearPipeStallByReference(pb->usbReference);
- }
-
- pb->usbStatus = kAvailableStatus;
- }
-
- /************************************************************************************/
- //
- // USBReadData
- //
- // Transfers data to the applications buffer.
- //
- /************************************************************************************/
-
- UInt32 USBReadData(UInt8 *buf, UInt32 size)
- {
- UInt16 actlength;
- UInt8 *flplen = (UInt8 *)&actlength;
- UInt32 bufindx;
- UInt8 *inbuf;
-
- TraceMessage(0, kDrvName"- Entering USBReadData");
-
- inbuf = RemoveFromRcvList(&bufindx);
-
- if (inbuf == nil)
- {
- actlength = 0;
- } else {
- flplen[0] = inbuf[1];
- flplen[1] = inbuf[0];
- if (size < actlength)
- {
- actlength = 0;
- } else {
- BlockMoveData(&inbuf+2, buf, actlength);
- }
- }
-
- return actlength;
-
- }
-
- /************************************************************************************/
- //
- // USBGetAddress
- //
- // Gets the ethernet address from the hardware (actually the functional descriptor).
- //
- /************************************************************************************/
-
- void USBGetAddress(UInt32 *count, UInt8 *buf)
- {
-
- TraceMessage(0, kDrvName"- Entering USBGetAddress");
-
- *count = 6;
- BlockMoveData(&gEnetGlobals.bMACAddress, buf, 6);
-
- }
-
- /************************************************************************************/
- //
- // USBSetisr
- //
- // Sets the isr and cookie for the receiver
- //
- /************************************************************************************/
-
- void USBSetisr(ProcPtr isr, void *cookie)
- {
-
- TraceMessage(0, kDrvName"- Entering USBSetisr");
-
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- isr - ", (UInt32)isr);
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- cookie - ", (UInt32)cookie);
-
- gGlobals->isr = isr;
- gGlobals->cookie = (UInt32)cookie;
- }
-
- /************************************************************************************/
- //
- // USBSetAddress
- //
- // Sets the temporary ethernet address.
- //
- /************************************************************************************/
-
- OSStatus USBSetAddress(UInt32 count, UInt8 *buf)
- {
- #pragma unused (count, buf)
-
- OSStatus err = noErr;
-
- TraceMessage(0, kDrvName"- Entering USBSetAddress");
-
- // Not supported by this driver
- err = ioErr;
-
- return err;
- }
-
- /************************************************************************************/
- //
- // multicastHandler
- //
- // Completion handler for USBSetMulticastFilters.
- //
- /************************************************************************************/
-
- static void multicastHandler(USBPB *pb)
- {
-
- TraceMessage(0, kDrvName"- Entering multicastHandler");
-
- if (pb->usbStatus != noErr)
- {
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- multicastHandler: Error", pb->usbStatus);
- // Return an async error
- (*gGlobals->ShimAsync) (gGlobals->ShimRef, EnetShim_Error, EnetHAL_SetMulticastFilters, pb->usbStatus);
- }
-
- PoolDeallocate(pb->usbBuffer);
- }
-
- /************************************************************************************/
- //
- // USBSetMulticastFilters
- //
- // Sets the multicast filters.
- //
- /************************************************************************************/
-
- OSStatus USBSetMulticastFilters(UInt32 numFilters, EnetAddressListPtr list)
- {
- OSStatus err = noErr;
- UInt16 countfilters=0;
- UInt16 filterLen;
- UInt16 i, indx=0;
- EnetAddressListPtr x;
- UInt8 *buf;
-
- TraceMessage(0, kDrvName"- Entering USBSetMulticastFilters");
-
- filterLen = numFilters*sizeof(EnetAddress);
- buf = PoolAllocateResident(filterLen, true);
-
- for (x=list; x; x=x->next)
- {
- countfilters += 1;
- for (i=0; i<=sizeof(EnetAddress)-1; i++)
- {
- buf[indx++] = x->address[i];
- if (indx > filterLen)
- break;
- }
- if (indx > filterLen)
- {
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Filter buffer overrun", indx);
- break;
- }
- }
-
- if (numFilters != countfilters)
- {
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Filters do not match", 0);
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Number from OT = ", numFilters);
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Number calculated = ", countfilters);
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Filters", (UInt32)list);
- if (list != nil)
- LogData(kUSBNone, 32, (UInt8 *)list);
- }
-
- InitializePB(&gEnetGlobals.pb, gEnetGlobals.deviceRef, multicastHandler);
- gEnetGlobals.pb.pbVersion = kUSBCurrentPBVersion;
- gEnetGlobals.pb.usb.cntl.BMRequestType = USBMakeBMRequestType(kUSBOut, kUSBVendor, kUSBDevice);
-
- gEnetGlobals.pb.usb.cntl.BRequest = kSet_Ethernet_Multicast_Filter;
- gEnetGlobals.pb.usbReference = gEnetGlobals.deviceRef;
- gEnetGlobals.pb.usbFlags = 0;
- gEnetGlobals.pb.usbClassType = 0;
- gEnetGlobals.pb.usb.cntl.WValue = numFilters;
- gEnetGlobals.pb.usb.cntl.WIndex = 0;
- gEnetGlobals.pb.usbReqCount = indx;
- gEnetGlobals.pb.usbBuffer = buf;
-
- noteError(kDrvName"- Driver setting Multicast Filters");
- if(immediateError(err = USBDeviceRequest(&gEnetGlobals.pb)))
- {
- USBExpertFatalError(gEnetGlobals.deviceRef, err, gEnetGlobals.errorString, 0);
- }
-
- if (err > noErr)
- err = noErr;
- return err;
- }
-
- /************************************************************************************/
- //
- // packetfilterHandler
- //
- // Completion handler for USBSetPacketfilter.
- //
- /************************************************************************************/
-
- static void packetfilterHandler(USBPB *pb)
- {
-
- TraceMessage(0, kDrvName"- Entering packetfilterHandler");
-
- if (pb->usbStatus != noErr)
- {
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- packetfilterHandler: Error", pb->usbStatus);
- // Return an async error
- (*gGlobals->ShimAsync) (gGlobals->ShimRef, EnetShim_Error, EnetHAL_SetMulticastFilters, pb->usbStatus);
- }
-
- }
-
- /************************************************************************************/
- //
- // USBSetPacketFilter
- //
- // Sets the packet filter.
- //
- /************************************************************************************/
-
- OSStatus USBSetPacketFilter(UInt16 *filter)
- {
- OSStatus err = noErr;
-
- TraceMessage(0, kDrvName"- Entering USBSetPacketFilter");
-
- InitializePB(&gEnetGlobals.pb, gEnetGlobals.deviceRef, packetfilterHandler);
- gEnetGlobals.pb.pbVersion = kUSBCurrentPBVersion;
- gEnetGlobals.pb.usb.cntl.BMRequestType = USBMakeBMRequestType(kUSBOut, kUSBVendor, kUSBDevice);
-
- gEnetGlobals.pb.usb.cntl.BRequest = kSet_Ethernet_Packet_Filter;
- gEnetGlobals.pb.usbReference = gEnetGlobals.deviceRef;
- gEnetGlobals.pb.usbFlags = 0;
- gEnetGlobals.pb.usbClassType = 0;
- gEnetGlobals.pb.usb.cntl.WValue = *filter;
- gEnetGlobals.pb.usb.cntl.WIndex = 0;
- gEnetGlobals.pb.usbReqCount = 0;
- gEnetGlobals.pb.usbBuffer = nil;
-
- noteError(kDrvName"- Driver setting packet filter");
- if(immediateError(err = USBDeviceRequest(&gEnetGlobals.pb)))
- {
- USBExpertFatalError(gEnetGlobals.deviceRef, err, gEnetGlobals.errorString, 0);
- }
-
- if (err > noErr)
- err = noErr;
- return err;
- }
-
- /************************************************************************************/
- //
- // TimeoutPrevRequest
- //
- // Timeout for all sync. requests.
- //
- /************************************************************************************/
-
- Boolean TimeoutPrevRequest(void)
- {
- USBPB *pb = &syncPB;
- AbsoluteTime startTime;
- Duration elapsedTime;
-
- if (pb->usbStatus == kAvailableStatus)
- return false;
-
- startTime = UpTime();
-
- while (pb->usbStatus != kAvailableStatus){
- elapsedTime = AbsoluteDeltaToDuration(UpTime(), startTime);
- if (elapsedTime < 0) elapsedTime = elapsedTime/(-1000); // make sure it's in milliseconds
- if (elapsedTime > 100*durationMillisecond){
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- TimeoutPrevRequest: Timeout - aborting", 0);
- USBAbortPipeByReference(pb->usbReference);
- return true;
- }
- }
- return false;
- }
-
- /************************************************************************************/
- //
- // TimeoutStallRequest
- //
- // Timeout for all stall clear requests.
- //
- /************************************************************************************/
-
- Boolean TimeoutStallRequest(void)
- {
- USBPB *pb = &stallPB;
- AbsoluteTime startTime;
- Duration elapsedTime;
-
- if (pb->usbStatus == kAvailableStatus)
- return false;
-
- startTime = UpTime();
-
- while (pb->usbStatus != kAvailableStatus){
- elapsedTime = AbsoluteDeltaToDuration(UpTime(), startTime);
- if (elapsedTime < 0) elapsedTime = elapsedTime/(-1000); // make sure it's in milliseconds
- if (elapsedTime > 100*durationMillisecond){
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- TimeoutStallRequest: Timeout - aborting", 0);
- USBAbortPipeByReference(pb->usbReference);
- return true;
- }
- }
- return false;
- }
-
- /************************************************************************************/
- //
- // TimeoutDelayRequest
- //
- // Timeout for delay requests.
- //
- /************************************************************************************/
-
- Boolean TimeoutDelayRequest(void)
- {
- USBPB *pb = &delayPB;
- AbsoluteTime startTime;
- Duration elapsedTime;
-
- if (pb->usbStatus == kAvailableStatus)
- return false;
-
- startTime = UpTime();
-
- while (pb->usbStatus != kAvailableStatus){
- elapsedTime = AbsoluteDeltaToDuration(UpTime(), startTime);
- if (elapsedTime < 0) elapsedTime = elapsedTime/(-1000); // make sure it's in milliseconds
- if (elapsedTime > 100*durationMillisecond){
- return true;
- }
- }
- return false;
- }
-
- /************************************************************************************/
- //
- // USBEnetDriverEntry
- //
- // Initializes and starts the whole show.
- //
- /************************************************************************************/
-
- OSStatus USBEnetDriverEntry(USBDeviceRef device, USBDeviceDescriptor *desc)
- {
- static Boolean beenThereDoneThat = false;
- static USBDeviceDescriptor ourDeviceDescriptor;
- UInt16 i;
-
- // DebugMessage(kDrvName"- Entering EnetDriverEntry - Sync. point");
- TraceMessage(0, kDrvName"- Entering EnetDriverEntry");
-
- if(beenThereDoneThat)
- {
- StatusMessage(device, kDrvName"- USBEnetDriverEntry: Driver called second time", 0);
- return -1;
- }
- beenThereDoneThat = true;
-
- ourDeviceDescriptor = *desc;
- gEnetGlobals.deviceDescriptor = &ourDeviceDescriptor;
- gEnetGlobals.deviceRef = device;
-
- InitializePB(&syncPB, device, syncCompletion);
- InitializePB(&stallPB, device, stallHandler);
- stallPB.usbStatus = kAvailableStatus;
- InitializePB(&delayPB, device, delayHandler);
- delayPB.usbStatus = kAvailableStatus;
-
- InitializePB(&gEnetGlobals.pb, device, ConfigurationHandler);
- gEnetGlobals.pb.usbRefcon = kCommConfig; // Where we start from (kReset used for errors)
- gEnetGlobals.pb.usbBuffer = nil;
-
- gEnetGlobals.retries = 3; // Let's keep it reasonable for now
-
- for (i=0; i<kNumInBufs; i++)
- {
- gGlobals->inBuf[i].avail = 0; // clear it
- gGlobals->inBuf[i].bufPB.usbBuffer = gGlobals->inBuf[i].Buffer; // assign input buffer
- gGlobals->inBuf[i].qLink = nil;
- gGlobals->inBuf[i].indx = i;
- }
-
- gGlobals->rcvq.qFlags = 0; // Clear the receive queue
- gGlobals->rcvq.qHead = nil;
- gGlobals->rcvq.qTail = nil;
-
- for (i=0; i<kNumOutBufs; i++) // We use the same buffer struct
- { // but no send queue is used
- gGlobals->outBuf[i].avail = kAvailable; // make it available
- gGlobals->outBuf[i].bufPB.usbBuffer = gGlobals->outBuf[i].Buffer; // assign output buffer
- gGlobals->outBuf[i].qLink = nil;
- gGlobals->outBuf[i].indx = i;
- }
-
- CheckUSBVersion();
- ConfigurationHandler(&gEnetGlobals.pb);
-
- if (configerr == 1) // pending's ok
- {
- return noErr;
- } else {
- return configerr;
- }
- }
-
- /************************************************************************************/
- //
- // InitializePB
- //
- // Initial a parameter block.
- //
- /************************************************************************************/
-
- static void InitializePB(USBPB *pb, USBDeviceRef ref, USBCompletion handler)
- {
- pb->pbVersion = kUSBCurrentPBVersion;
- pb->pbLength = sizeof(*pb);
- pb->usbReference = ref;
- pb->usbCompletion = handler;
- pb->usbStatus = noErr;
-
- }
-
- /************************************************************************************/
- //
- // readCompletion
- //
- // Completion handler for USB reads.
- //
- /************************************************************************************/
-
- void readCompletion(USBPB *pb)
- {
-
- TraceMessage(0,kDrvName"- Entering readCompletion");
-
- if (pb->usbStatus != kUSBAbortedError) // are we being asked to quit?
- {
- if (pb->usbStatus == noErr)
- {
- if (pb->usbActCount > 2)
- {
- TraceMessage(0,kDrvName"- readCompletion live data");
- LogData(kUSBIn, pb->usbActCount, pb->usbBuffer);
-
- AddToRcvList(pb->usbRefcon);
- // Let's tell someone
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Calling with isr - ", (UInt32)gGlobals->isr);
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Calling with cookie - ", gGlobals->cookie);
- if (gGlobals->isr)
- (*gGlobals->isr) (gGlobals->cookie);
- } else {
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- packet discarded, length = ", pb->usbActCount);
- USBStartReadPolling(pb->usbRefcon);
- }
- } else {
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- readCompletion: Error ", pb->usbStatus);
-
- if ((pb->usbStatus != kUSBUnderRunErr) && errCount++ < 10)
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- readCompletion: Error ", pb->usbStatus);
-
- if (pb->usbStatus == kUSBEndpointStallErr)
- {
- // ClearDevice();
- }
- USBClearPipeStallByReference(gEnetGlobals.bulkIn);
-
- LogData(kUSBIn, pb->usbActCount, pb->usbBuffer);
-
- if (pb->usbActCount > 2)
- {
- AddToRcvList(pb->usbRefcon);
- // Let's tell someone
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Calling with isr - ", (UInt32)gGlobals->isr);
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- Calling with cookie - ", gGlobals->cookie);
- if (gGlobals->isr)
- (*gGlobals->isr) (gGlobals->cookie);
- } else {
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- packet discarded, length = ", pb->usbActCount);
- USBStartReadPolling(pb->usbRefcon);
- }
- }
- }
- }
-
- /************************************************************************************/
- //
- // USBStartupRead
- //
- // Start the USB read mechanism for each buffer.
- //
- /************************************************************************************/
-
- void USBStartupRead(void)
- {
- UInt32 bufInindx;
-
- TraceMessage(0,kDrvName"- Entering USBStartupRead");
-
- // Find a buffer
- for (bufInindx=0; bufInindx<kNumInBufs; bufInindx++)
- {
- USBStartReadPolling(bufInindx);
- }
-
- }
-
- /************************************************************************************/
- //
- // USBStartReadPolling
- //
- // Start the USB read mechanism.
- //
- /************************************************************************************/
-
- void USBStartReadPolling(UInt32 bufindx)
- {
- OSStatus status;
-
- TraceMessage(0,kDrvName"- Entering USBStartReadPolling");
-
- if (gEnetGlobals.bulkIn)
- {
- gGlobals->inBuf[bufindx].avail = 0;
-
- InitializePB(&gGlobals->inBuf[bufindx].bufPB, gEnetGlobals.bulkIn, readCompletion);
- gGlobals->inBuf[bufindx].bufPB.usbRefcon = bufindx;
- gGlobals->inBuf[bufindx].bufPB.usbReqCount = kBufferSize+2;
- if(immediateError(status = USBBulkRead(&gGlobals->inBuf[bufindx].bufPB)))
- {
- USBExpertFatalError(gEnetGlobals.deviceRef, status, kDrvName"- USBStartReadPolling: Couldn't start read", bufindx);
- return;
- }
- }
- }
-
- /************************************************************************************/
- //
- // USBStopReadPolling
- //
- // You guessed it stop the USB read mechanism.
- //
- /************************************************************************************/
-
- void USBStopReadPolling()
- {
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- USBStopReadPolling: Aborting Bulk-in pipe", 0);
-
- if (gEnetGlobals.bulkIn)
- {
- USBAbortPipeByReference(gEnetGlobals.bulkIn);
- gGlobals->rcvq.qHead = nil;
- gGlobals->rcvq.qTail = nil;
- }
- }
-
- /************************************************************************************/
- //
- // writeCompletion
- //
- // USB write completion handler.
- //
- /************************************************************************************/
-
- void writeCompletion(USBPB *pb)
- {
- ShimEnetGlobals *globals = gGlobals;
-
- TraceMessage(0, kDrvName"- Entering writeCompletion");
-
- if (pb->usbStatus != kUSBAbortedError) // are we being asked to quit?
- {
- if (pb->usbStatus != noErr)
- {
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- writeCompletion: Error", pb->usbStatus);
-
- // Return an async error
- (*gGlobals->ShimAsync) (gGlobals->ShimRef, EnetShim_Error, EnetHAL_Write, pb->usbStatus);
-
- if (pb->usbStatus == kUSBEndpointStallErr)
- {
- ClearDevice();
- }
- USBClearPipeStallByReference(pb->usbReference);
- }
- }
- gGlobals->outBuf[pb->usbRefcon].avail = kAvailable;
- }
-
- /************************************************************************************/
- //
- // USBEnetWrite
- //
- // USB Ethernet write routine.
- //
- /************************************************************************************/
-
- OSStatus USBEnetWrite(UInt32 count, UInt8 *buf)
- {
- OSStatus status = noErr;
- UInt8 *datalen = (unsigned char *)&count;
- UInt32 bufindx;
- Boolean writeok = false;
-
- TraceMessage(0, kDrvName"- Entering USBEnetWrite");
-
- // Find a buffer
- for (bufindx=0; bufindx<kNumOutBufs; bufindx++)
- {
- if (gGlobals->outBuf[bufindx].avail == kAvailable)
- {
- writeok = true;
- gGlobals->outBuf[bufindx].avail = kUnavailable;
- break;
- }
- }
- if (!writeok)
- {
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- USBEnetWrite: No buffers available", bufindx);
- status = ioErr;
- gGlobals->outBuf[bufindx].avail = kAvailable;
- } else {
- if (!gEnetGlobals.bulkOut)
- {
- status = ioErr;
- gGlobals->outBuf[bufindx].avail = kAvailable;
- } else {
- if ((count+2) > kBufferSize)
- {
- status = ioErr;
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- USBEnetWrite: Data length error", count);
- gGlobals->outBuf[bufindx].avail = kAvailable;
- } else {
- BlockMoveData(buf, &gGlobals->outBuf[bufindx].Buffer[2], count);
- gGlobals->outBuf[bufindx].Buffer[0] = datalen[3];
- gGlobals->outBuf[bufindx].Buffer[1] = datalen[2];
- InitializePB(&gGlobals->outBuf[bufindx].bufPB, gEnetGlobals.bulkOut, writeCompletion);
- gGlobals->outBuf[bufindx].bufPB.usbRefcon = bufindx;
- gGlobals->outBuf[bufindx].bufPB.usbReqCount = count+2;
-
- LogData(kUSBOut, gGlobals->outBuf[bufindx].bufPB.usbReqCount, gGlobals->outBuf[bufindx].bufPB.usbBuffer);
-
- if(immediateError(status = USBBulkWrite(&gGlobals->outBuf[bufindx].bufPB)))
- {
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- USBEnetWrite: Couldn't start write", status);
- gGlobals->outBuf[bufindx].avail = kAvailable;
- } else {
- status = noErr;
- }
- }
- }
- }
- return status;
- }
-
- /************************************************************************************/
- //
- // KillUSBIO
- //
- // Kill all USB io operations in progress.
- //
- /************************************************************************************/
-
- void KillUSBIO(void)
- {
-
- TraceMessage(0, kDrvName"- Entering KillUSBIO");
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- KillUSBIO: Killing all USB IO", 0);
-
- if (gEnetGlobals.bulkIn){
- USBAbortPipeByReference(gEnetGlobals.bulkIn);
- gEnetGlobals.bulkIn = nil;
- }
- if (gEnetGlobals.bulkOut){
- USBAbortPipeByReference(gEnetGlobals.bulkOut);
- gEnetGlobals.bulkOut = nil;
- }
- if (gEnetGlobals.interrupt){
- USBAbortPipeByReference(gEnetGlobals.interrupt);
- gEnetGlobals.interrupt = nil;
- }
- if (gGlobals)
- {
- gGlobals->rcvq.qHead = nil;
- gGlobals->rcvq.qTail = nil;
- }
- }
-
- /************************************************************************************/
- //
- // DoDelay
- //
- // Set up to do a USB delay.
- //
- /************************************************************************************/
-
- void DoDelay(void)
- {
-
- TraceMessage(0, kDrvName"- Entering DoDelay");
-
- if (TimeoutDelayRequest())
- return;
-
- delayPB.usbStatus = noErr;
- delayPB.usbReference = gEnetGlobals.deviceRef;
- delayPB.usbRefcon = 1;
- delayPB.usbReqCount = 5; // ~5ms (actually 5 frames at 1ms each)
- USBDelay(&delayPB);
- }
-
- /************************************************************************************/
- //
- // delayHandler
- //
- // Completion handler for USB delay.
- //
- /************************************************************************************/
-
- static void delayHandler(USBPB *pb)
- {
-
- TraceMessage(0, kDrvName"- Entering delayHandler");
-
- if (pb->usbStatus != noErr)
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- delayHandler: Error", pb->usbStatus);
-
- pb->usbStatus = kAvailableStatus;
- }
-
- /************************************************************************************/
- //
- // ClearDevice
- //
- // Set up and send ClearFeature request (for stall).
- //
- /************************************************************************************/
-
- void ClearDevice(void)
- {
-
- TraceMessage(0, kDrvName"- Entering ClearDevice");
-
- if (TimeoutStallRequest())
- return;
-
- stallPB.usbStatus = noErr;
- stallPB.usbReference = gEnetGlobals.deviceRef;
- stallPB.usbRefcon = 1;
- stallHandler(&stallPB);
- }
-
- /************************************************************************************/
- //
- // stallHandler
- //
- // Completion handler for clear device/stall.
- //
- /************************************************************************************/
-
- static void stallHandler(USBPB *pb)
- {
- OSStatus err = 0;
-
- TraceMessage(0, kDrvName"- Entering StallHandler");
-
- if (pb->usbStatus != noErr)
- {
- USBExpertFatalError(gEnetGlobals.deviceRef, pb->usbStatus, kDrvName"- stallHandler: Error", pb->usbRefcon);
- return;
- }
-
- // May have to do more here (like find out which endpoint is stalled) which is why an FSM is used
- do{switch(pb->usbRefcon++)
- {
- case kEndpointStall:
- pb->usbStatus = noErr;
- pb->usbReference = gEnetGlobals.deviceRef;
- pb->pbVersion = kUSBCurrentPBVersion;
- pb->usb.cntl.BMRequestType = USBMakeBMRequestType(kUSBOut, kUSBStandard, kUSBEndpoint);
-
- pb->usb.cntl.BRequest = kUSBRqClearFeature;
- pb->usb.cntl.WValue = 0; // Endpoint stall
- pb->usb.cntl.WIndex = 0;
- pb->usbReqCount = 0;
- pb->usbBuffer = nil;
-
- if(immediateError(err = USBDeviceRequest(pb)))
- {
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- stallHandler: Device Req. error", err);
- }
- break;
-
- case kStallDone:
- // stall should now be cleared
- if (pb->usbStatus != noErr)
- USBExpertFatalError(gEnetGlobals.deviceRef, pb->usbStatus, kDrvName"- stallHandler: stall completion error for bRequest", pb->usb.cntl.BRequest);
-
- pb->usbStatus = kAvailableStatus;
- break;
-
- default:
- StatusMessage(gEnetGlobals.deviceRef, kDrvName"- stallHandler: Internal Error unused case", (pb->usbRefcon-1));
- break;
- }
- break; /* only execute once, unless continue used */
- }while(1); /* so case can be reentered with a continue */
- }
-
- /************************************************************************************/
- //
- // Asciify
- //
- // Convert to Ascii character
- //
- /************************************************************************************/
-
- UInt8 Asciify(UInt8 i)
- {
-
- i &= 0xF;
- if ( i < 10 )
- return( '0' + i );
- else return( 55 + i );
-
- }
-
- #if ((DebugOn > 0) && (LogOn > 0))
-
- #define dumplen 32 // Set this to the number of bytes to dump and the rest should work out correct
-
- #define buflen ((dumplen*2)+dumplen)+3
- #define Asciistart (dumplen*2)+3
-
- /************************************************************************************/
- //
- // USBLogData
- //
- // Dumps the requested amount of data to the USB Expert log.
- //
- /************************************************************************************/
-
- void USBLogData(UInt8 Dir, UInt32 Count, UInt8 *buf)
- {
- UInt8 wlen, i, Aspnt, Hxpnt;
- UInt8 wchr;
- UInt8 LocBuf[buflen];
-
- for ( i=1; i<=buflen; i++)
- {
- LocBuf[i] = 0x20;
- }
-
- if (Dir == kUSBIn)
- {
- TraceMessage(1, kDrvName"- Read Complete");
- } else {
- if (Dir == kUSBOut)
- {
- TraceMessage(1, kDrvName"- Write");
- }
- }
-
- if (Count > dumplen)
- {
- wlen = dumplen;
- } else {
- wlen = Count;
- }
-
- if (wlen > 0)
- {
- Aspnt = Asciistart;
- Hxpnt = 1;
- for (i=1; i<=wlen; i++)
- {
- wchr = buf[i-1];
- LocBuf[Hxpnt++] = Asciify(wchr >> 4);
- LocBuf[Hxpnt++] = Asciify(wchr);
- if ((wchr < 0x20) || (wchr == 0x7F)) // Non printable characters
- {
- LocBuf[Aspnt++] = 0x2E; // Replace with a period
- } else {
- LocBuf[Aspnt++] = wchr;
- }
- }
- LocBuf[0] = (wlen + Asciistart) + 1;
- TraceMessage(1, LocBuf);
- } else {
- TraceMessage(1, kDrvName"- USBLogData: No data - Actual count=0");
- }
- }
- #endif
-
- /***********************************************************************************/
- // Function: InitDriver(CFragInitBlock *init)
- // Description: This routine saves our connection id
- //
- // Input: init (unused)
- // Output: noErr
- /***********************************************************************************/
-
- OSErr InitDriver(CFragInitBlock *init);
- OSErr InitDriver(CFragInitBlock *init)
- {
- OSErr err = noErr;
-
- TraceMessage(0, kCRMName"- Entering initDriver");
-
- ConnID = init->connectionID;
-
- return err;
- }